<?php

$mcrp = './';
include_once('functions/session.php');
include_once('functions/nanofight.php');

$head = '<script type="text/javascript" src="nano/nano.js"></script>
<script type="text/javascript" src="nano/nanomenu.js"></script>
<script type="text/javascript" src="nano/list.js"></script>
<link href="nano/nano.css" rel="stylesheet" media="screen" type="text/css" title="style" />';
include('skin/default.php');
include('skin/header.php');

if(isset($_GET['exec']) && is_numeric($_GET['exec']) && (int)$_GET['exec'] == $_GET['exec'])
{
	$execinfos = NanoPrepareFightExec($_GET['exec']);
	if($execinfos === false) unset($execinfos);
}

?>

<div class="mc_box">
	<h3><a href="nano.php">Nanocontest</a> :: <?php if(isset($execinfos)) { ?><a href="nanofight.php">Combats</a> :: <?php } ?>Machine Virtuelle</h3>
	<?php if($mcuser->id == 0) { ?>
	<div><center><img src="img/stop-trackback.png"><br/>Vous devez être connecté pour accéder à cette page.<br />Si vous n'êtes pas encore inscrit, rendez-vous sur la page d'<a href="register.php">inscription</a>.</center></div>
	<?php } else { ?>
	<?php if(isset($execinfos)) { ?>
	<div style="width:95%;margin:auto;"><br />Le vainqueur est déterminé grâce à une série statistique de plusieurs combats exécutés sur le serveur. Le résultat de celui-ci peut donc différer de celui du serveur.</div>
	<?php } ?>
	<div id="mc_nvm_options">
		<div id="mc_nvm_m1" class="mc_nvm_menu_item" onmouseover="NanoDispMenu(this);" onmouseout="NanoHideMenu(this);">
			<div class="mc_nvm_title">Affichage</div>
			<div id="mc_nvm_m1u" class="mc_nvm_menu_u" style="display:none;">
				<div class="mc_nvm_umenu_item" onclick="NanoResize(2);">Petit</div>
				<div class="mc_nvm_umenu_item" onclick="NanoResize(3);">Grand</div>
			</div>
		</div>
		<?php if(!isset($execinfos)) { ?>
		<div id="mc_nvm_m2" class="mc_nvm_menu_item" onmouseover="NanoDispMenu(this);" onmouseout="NanoHideMenu(this);">
			<div class="mc_nvm_title">Programmes</div>
			<div id="mc_nvm_m2u" class="mc_nvm_menu_u" style="display:none;">
				<div class="mc_nvm_umenu_item" onclick="NanoLoadProgram('');">Charger un programme</div>
				<div class="mc_nvm_umenu_item" onclick="NanoLoadLastProgram();">Charger le dernier programme compilé</div>
			</div>
		</div>
		<?php } ?>
		<div id="mc_nvm_m3" class="mc_nvm_menu_item" onmouseover="NanoDispMenu(this);" onmouseout="NanoHideMenu(this);">
			<div class="mc_nvm_title">Exécution</div>
			<div id="mc_nvm_m3u" class="mc_nvm_menu_u" style="display:none;">
				<div class="mc_nvm_umenu_item" onclick="NanoStartExecution();">Lancer l'exécution</div>
				<div class="mc_nvm_umenu_item" onclick="NanoClock();">Clock</div>
				<div class="mc_nvm_umenu_item" onclick="NanoStopExecution();">Stopper l'exécution</div>
				<div class="mc_nvm_umenu_item">Vitesse : <span class="mc_nvm_inline_item" onclick="NanoDecSpeed();">-</span><span id="mc_nvm_speed">1</span><span class="mc_nvm_inline_item" onclick="NanoIncSpeed();">+</span></div>
				<?php if($mcuser->isadmin) { ?><div class="mc_nvm_umenu_item" onclick="NanoTestServerExec();">Exécuter sur le serveur</div>
				<script language="javascript" type="text/javascript">
					function NanoTestServerExec()
					{
						var cmd = {};
						cmd.v = 'do=<?php if(!isset($execinfos)) echo 'test'; ?>exec&maxthread=3';
						nanovm.prgm.foreach(function(p, cmd) { cmd.v += '&player=' + p.name + '|' + p.id + '|' + p.addr + '|' + p.raw; }, cmd);
						GetPageContent('POST', 'http://www.microcontest.com/cgi-bin/nanovm.cgi', cmd.v, NanoCheckTestServerExec, null, true);
					}
					function NanoCheckTestServerExec(success, text, param)
					{
						if(success == false) return;
						alert(text);
					}
				</script><?php } ?>
			</div>
		</div>
		<div id="mc_nvm_m4" class="mc_nvm_menu_item" onmouseover="NanoDispMenu(this);" onmouseout="NanoHideMenu(this);">
			<div class="mc_nvm_title">Machine</div>
			<div id="mc_nvm_m4u" class="mc_nvm_menu_u" style="display:none;">
				<div class="mc_nvm_umenu_item" onclick="NanoReset();">Réinitialiser</div>
			</div>
		</div>
		<?php if(!isset($execinfos)) { ?>
		<div id="mc_nvm_m5" class="mc_nvm_menu_item" onmouseover="NanoDispMenu(this);" onmouseout="NanoHideMenu(this);">
			<div class="mc_nvm_title">Debug</div>
			<div id="mc_nvm_m5u" class="mc_nvm_menu_u" style="display:none;">
				<div class="mc_nvm_umenu_item" onclick="DebugDisplayRegisters(this);">Afficher les registres</div>
			</div>
		</div>
		<?php } ?>
	</div>
	<?php if(isset($execinfos['do']) && $execinfos['do'] == 'serverexec') { ?>
	<div id="nc_server_exec"><div><span id="nc_server_exec_infos">Exécution en cours sur le serveur...</span><a href="#" onclick="ToggleDisplay('nc_execlog');return false;"> (Log d'exécution)</a></div><pre id="nc_execlog" style="display:none;">En attente</pre></div>
	<script language="javascript" type="text/javascript">NanoServerExec();</script>
	<?php } ?>
	<div id="nc_vm_player_list"><div id="nc_cycles" class="nc_fight_user">0 cycles</div><?php if(isset($execinfos)) { foreach($execinfos['players'] as $p) echo '<div class="nc_fight_user" id="ncvmp_' . $p[0]->id . '">' . $p[0]->name . '</div>'; } ?></div>
	<center><canvas id="mc_nano_vm">HTML5 non supporté.</canvas></center>
	<div id="nc_debug_reg"></div>
	<?php if(isset($execinfos)) { ?>
	<div class="nc_fight_comment"><div class="nc_comment_div"><form method="POST"><input name="comment" style="width:80%;color:#555;" value="Ajouter un commentaire" onclick="this.value='';this.style.color='';"><button style="float:right;" type="submit" class="nc_button">Ajouter</button><input name="id" type="hidden" value="<?php echo $execinfos['id']; ?>"></form></div><div class="nc_comment_div"><?php $comment = GetFightComment($execinfos['id']); echo $comment['comment']; ?></div></div>
	<?php } ?>
	<script language="javascript" type="text/javascript">
	<?php if(isset($execinfos))
	{
		echo 'NanoInitVMExec({';
		$object = '';
		foreach($execinfos['players'] as $p)
		{
			$object .= '\'' . $p[0]->name . '\':{\'id\':' . $p[0]->id . ', \'code\':\'' . $p['code'] . '\'},';
		}
		echo rtrim($object, ',');
		echo '});';
	}
	else echo 'NanoInitVM();' ?>
	</script>
	<?php } ?>
</div>

<?php

include('skin/footer.php');

?>